Skip to content
QtWS25 Last Chance
  • 0 Votes
    1 Posts
    3k Views
    No one has replied
  • Drag and Drop to a QTreeWidget subclass

    Unsolved General and Desktop drag and drop qtreewidget
    2
    0 Votes
    2 Posts
    837 Views
    SGaistS
    Hi, You should share the code of your MyWidgetTree class. That way it will make things easier to analyse.
  • QTableWidget move row

    Unsolved General and Desktop qtablewidget drag and drop
    4
    0 Votes
    4 Posts
    4k Views
    UnitScanU
    Ok, now works perfectly, but I would ask if you can change the animation drag'n'drop between the lines. Currently, during the row drag it appears a copy of a line anchored to the mouse cursor. You can change this animation? #include <QWidget> #include <QTableWidget> #include <QDropEvent> #include <QDragMoveEvent> #include <QDropEvent> #include <QMimeData> #include <QDebug> #include <QKeyEvent> #ifndef DTABLEWIDGET_H #define DTABLEWIDGET_H class DTableWidget : public QTableWidget { Q_OBJECT public: DTableWidget(QWidget *parent = 0); public slots: signals: void keyboard(QKeyEvent *event); void dropped(const QMimeData* mimeData = 0); void moved(int old_row, int new_row); protected: void dragEnterEvent(QDragEnterEvent *event); void dragMoveEvent(QDragMoveEvent *event); void dragLeaveEvent(QDragLeaveEvent *event); void dropEvent(QDropEvent *event); void keyPressEvent(QKeyEvent* event); private: }; #endif #include <QtGui> #include "dtablewidget.h" #include "nofocusproxystyle.cpp" DTableWidget::DTableWidget(QWidget *parent) : QTableWidget(parent) { //set widget default properties: setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); setDropIndicatorShown(true); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); setEditTriggers(QAbstractItemView::NoEditTriggers); setDragDropMode(QAbstractItemView::DropOnly); setAlternatingRowColors(true); setSelectionBehavior(QAbstractItemView::SelectRows); setShowGrid(false); setAcceptDrops(true); setWordWrap(false); setStyleSheet("selection-background-color: yellow;" "selection-color: #002041;" "font-size: 75%;" ); setStyle(new NoFocusProxyStyle()); } void DTableWidget::dragEnterEvent(QDragEnterEvent *event) { event->acceptProposedAction(); } void DTableWidget::dragMoveEvent(QDragMoveEvent *event) { event->acceptProposedAction(); } void DTableWidget::dropEvent(QDropEvent *event) { event->acceptProposedAction(); if (event->mimeData()->urls().size() > 0) emit dropped(event->mimeData()); else { QPoint old_coordinates = QPoint(-1,-1); int dropAction = event->dropAction(); if(currentItem() != NULL) //Check if user is not accessing empty cell { old_coordinates = QPoint(currentItem()->row(), currentItem()->column()); } QTableWidget::dropEvent(event); qDebug() << "Detected drop event..."; if(this->itemAt(event->pos().x(), event->pos().y()) != NULL && old_coordinates != QPoint(-1, -1)) { qDebug() << "Drop Event Accepted."; qDebug() << "Source: " << old_coordinates.x() << old_coordinates.y() << "Destinition: " << this->itemAt( event->pos().x(), event->pos().y() )->row() << this->itemAt( event->pos().x(), event->pos().y() )->column() << "Type: " << dropAction; emit moved(old_coordinates.x(), itemAt( event->pos().x(), event->pos().y())->row()); } } } void DTableWidget::dragLeaveEvent(QDragLeaveEvent *event) { event->accept(); } void DTableWidget::keyPressEvent(QKeyEvent *event) { emit keyboard(event); }
  • 0 Votes
    2 Posts
    2k Views
    raven-worxR
    @Yohdu said: My second and main question is then : how to implement correct drag and drop operations with custom QStandardItem which needs to own custom data ? With the standard item widgets/models you can't drag-n-drop custom item roles. This makes sense, since the model doesn't know which UserRole+ was set. It would need to iterate all possible item-role values when creating the drop-data. The only solution i see is to create your own custom model and reimplement the needed drag-n-drop methods. This also give you full control over your data structure storing the data inside the model. This is more work, but you will learn a lot and gain some performance (by avoiding the standard-item classes)
  • 0 Votes
    3 Posts
    2k Views
    l1q1d56L
    Hi @Joel-Bodenmann , thank you, I got the point about the item delegate but I see two issues: implement a drag and drop listview because I ended up with empty lines (on the list view) and fields on the widget mapper: https://s32.postimg.org/47yyrq7id/Untitled.png implement a delegate that paint a checkbox with custom pixmap, the input with number and the input with text.
  • Drag and Drop into empty widget space

    Solved General and Desktop drag and drop qwidget drop drag
    5
    0 Votes
    5 Posts
    3k Views
    mrjjM
    @Vagabond Heh. good work. Just as a note: you can use event filters to implement drag and drop on other widgets if no other need to subclass them. http://ynonperek.com/course/qt/event-filter2.html But for your case, subclassing QGroupBox is super.
  • Drag tabs between QTabWidgets

    Solved General and Desktop qtabwidget qtabbar tab drag drag and drop
    6
    0 Votes
    6 Posts
    9k Views
    A
    Had a similar Issue. Was able to find a solution. Below is a generic PyQt5 example that solves the problem using right click. import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * class Tabs(QTabWidget): def __init__(self, parent): super().__init__(parent) self.parent = parent self.setAcceptDrops(True) self.tabBar = self.tabBar() self.tabBar.setMouseTracking(True) self.indexTab = None self.setMovable(True) self.addTab(QWidget(self), 'Tab One') self.addTab(QWidget(self), 'Tab Two') def mouseMoveEvent(self, e): if e.buttons() != Qt.RightButton: return globalPos = self.mapToGlobal(e.pos()) tabBar = self.tabBar posInTab = tabBar.mapFromGlobal(globalPos) self.indexTab = tabBar.tabAt(e.pos()) tabRect = tabBar.tabRect(self.indexTab) pixmap = QPixmap(tabRect.size()) tabBar.render(pixmap,QPoint(),QRegion(tabRect)) mimeData = QMimeData() drag = QDrag(tabBar) drag.setMimeData(mimeData) drag.setPixmap(pixmap) cursor = QCursor(Qt.OpenHandCursor) drag.setHotSpot(e.pos() - posInTab) drag.setDragCursor(cursor.pixmap(),Qt.MoveAction) dropAction = drag.exec_(Qt.MoveAction) def dragEnterEvent(self, e): e.accept() if e.source().parentWidget() != self: return print(self.indexOf(self.widget(self.indexTab))) self.parent.TABINDEX = self.indexOf(self.widget(self.indexTab)) def dragLeaveEvent(self,e): e.accept() def dropEvent(self, e): print(self.parent.TABINDEX) if e.source().parentWidget() == self: return e.setDropAction(Qt.MoveAction) e.accept() counter = self.count() if counter == 0: self.addTab(e.source().parentWidget().widget(self.parent.TABINDEX),e.source().tabText(self.parent.TABINDEX)) else: self.insertTab(counter + 1 ,e.source().parentWidget().widget(self.parent.TABINDEX),e.source().tabText(self.parent.TABINDEX)) class Window(QWidget): def __init__(self): super().__init__() self.TABINDEX = 0 tabWidgetOne = Tabs(self) tabWidgetTwo = Tabs(self) layout = QHBoxLayout() self.moveWidget = None layout.addWidget(tabWidgetOne) layout.addWidget(tabWidgetTwo) self.setLayout(layout) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())
  • 0 Votes
    1 Posts
    1k Views
    No one has replied
  • 0 Votes
    4 Posts
    2k Views
    mrjjM
    @tokafr Super. so it can copy files on its own ? Doc hinted it, but I was not sure.
  • 0 Votes
    4 Posts
    3k Views
    mrjjM
    HI so rootItem is your "drop" widget? Is the yellow on picture rootitem or the white one where mouse is? ( A=8 ) It does look correct so a bit odd. and void dragEnterEvent(QDragEnterEvent* event) Q_DECL_OVERRIDE; should be called when u leave this small area. I assume you are 100% sure that is not called and its not just the change to while part that is failing. I can't guess from this little code what could be wrong.sorry. Just for test, could you run my editor and see it does indeed say "drag leave" when u drag from the list on the left to center. https://www.dropbox.com/s/ikra7pm161lzf56/DMDesigner.zip?dl=0
  • QTreeView drop not working

    Solved General and Desktop qtreeview drop drag and drop
    2
    0 Votes
    2 Posts
    4k Views
    Joel BodenmannJ
    This issue is now resolved. It turned out to be a problem in the model: I forgot to tell the root (invalid parent) to accept drops too. This is done in the QAbstractItemModel::flags() implementation. The incorrect version was: Qt::ItemFlags MyModel::flags(const QModelIndex& index) const { if (!index.isValid()) { return 0; } return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled; } While this is how it should look like to allow drops in the root (to add new items without a parent): Qt::ItemFlags MyModel::flags(const QModelIndex& index) const { if (!index.isValid()) { return Qt::ItemIsDropEnabled; // Allow drops in the top-level (no parent) } return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled; }
  • Find out drop location of a QDrag

    Unsolved General and Desktop drag and drop
    2
    0 Votes
    2 Posts
    799 Views
    T
    Did you find the solution?
  • QTreeView internal drag'n'drop

    Unsolved General and Desktop qtreeview drag and drop
    3
    0 Votes
    3 Posts
    2k Views
    P
    Hi @Joel-Bodenmann Have you got any solution/feedback for your question?
  • 0 Votes
    10 Posts
    8k Views
    V
    Well, I found solution. It is very simple, all I need is to set default drop action for my view class as Qt::MoveAction and return copy and move in the suporrtedDropActions().
  • 0 Votes
    7 Posts
    4k Views
    Guy GizmoG
    @bsomervi Well, that's okay -- it's not that important that the indicator be there. Thanks for your help!
  • Drag and Drop within a ListView or a Column

    Unsolved QML and Qt Quick drag and drop
    1
    0 Votes
    1 Posts
    993 Views
    No one has replied
  • 0 Votes
    7 Posts
    6k Views
    6
    @raven-worx You're correct. I am now inserting a nullptr in the insertRows() function, and then later in setData I use the QList::replace() function to actually put a shape into that index. The reason it didn't work is that I used the pre-compiled Qt binaries. On the binaries, for some reason (probably that I don't know how to set it up properly), putting a breakpoint on any function that is defined in the source code does not trigger the debugger to stop there at all. I fixed that by deleting Qt, compiling it from the sources (with some headache), installing Qt Creator again and pointing it to the kit. Now I can put breakpoints into the source code and the debugger will actually stop on them, revealing that the call to setData actually does happen when dropping rows. After all this I came upon a solution that seems to work, I'd just like an opinion if this is a good way to go about it, it might need some polish. I figured I didn't want to reimplement the whole drag and drop system just to be able to pass some indexes through it, when I am already displaying the shape's names in the view, so I thought... to determine the index, I can just iterate through my list and find the shape that has the same name as the one that I clicked on. Here's what I do in setData: bool ShapeListModel::setData(const QModelIndex& index, const QVariant& value, int role) { //This role is used when double-clicking on an item if (index.isValid() && role == Qt::EditRole) { if (value.toString() != shapeList.at(index.row())->getName() && !value.toString().isEmpty()) { shapeList[index.row()]->setName(value.toString()); emit dataChanged(index, index); return true; } } //And this one when drag and dropping else if (role == Qt::DisplayRole) { Shape* shape {nullptr}; for (int i = 0; i < shapeList.size(); i++) { if (i == index.row()) { //do nothing } else { if (shapeList.at(i)->getName() == value.toString()) { shape = shapeList.at(i); if (shape) { shapeList.replace(index.row(), shape); return true; } } } } } return false; } So basically, I do a for loop on my shapeList to find the shape which has the name displayed in the QVariant "value" (the name that I clicked on in the view). Once I found it, I set a pointer to its address. Then I simply replace the contents of the empty row (the one with the nullptr) with the shape that I clicked on. Afterwards the call to removeRows takes care of the shape's original index. This works fine. The only requirement is that each shape has a unique name, which is probably a good idea to do anyways. The only thing I'm not sure about is the "else if (role == Qt::DisplayRole)", but according to the debugger that is the set role when drag and dropping. What do you think of this implementation?
  • Drag and Drop between QTableWidgets

    Solved General and Desktop qtablewidget drag and drop
    2
    1 Votes
    2 Posts
    1k Views
    O
    I finally solved the issue in reimplementing the dropEvent like that : if (event) { // Get QTableWidget QTableWidget *table = qobject_cast<QtableWidget *>(event->source()); // Decode Mime Data QString format = QLatin1String("application/x-qabstractitemmodeldatalist"); QByteArray encoded = event->mimeData()->data(format); QDataStream stream(&encoded, QIODevice::ReadOnly); // Decode format (row, column) to get row & column coordinates int row,column; stream >> row >> column; }
  • Hide default drag rendering

    Solved General and Desktop qtreeview qgraphicsscene drag and drop
    7
    0 Votes
    7 Posts
    3k Views
    Joel BodenmannJ
    Yes, of course... Sorry, it was a long day and I didn't pay enough attention. I now managed to implement a working version. I don't yet render the QGraphicsItem into a QPixmap but for the first version I am pretty happy with the fact that it just doesn't render the standard selection box thingy. My code: void WidgetsLibraryView::startDrag(Qt::DropActions supportedActions) { // Partly copied from Qt 5.5.5 sources QModelIndexList indexes = selectedIndexes(); if (indexes.count() > 0) { // Get a list of the supported MIMEs of the selected indexes QMimeData* data = model()->mimeData(indexes); if (!data) { return; } // Create the drag object QDrag* drag = new QDrag(this); drag->setMimeData(data); // Execute the drag drag->exec(supportedActions, Qt::CopyAction); } } Thank you for your help! Very appreciated.
  • drag and drop in QListWidget

    General and Desktop drag and drop qlistwidget
    6
    0 Votes
    6 Posts
    2k Views
    SGaistS
    The logic is the same, you have to handle the mime type used by Window's explorer when you drag the file on the widget.